﻿Imports Microsoft.VisualBasic.FileIO.FileSystem
Imports System.Text
Imports System.IO

Module AccessFiles

    Public DateSelected As New Date
    Public PathXmlFiles As String
    Public PathRegistriesFolder As String

    Public Sub CheckFolderRegistriesSolution()

        Dim TempPath As String = ""

        Directory.SetCurrentDirectory(Application.StartupPath)

        Do

            TempPath = Directory.GetCurrentDirectory()

            If Directory.GetLogicalDrives.Contains(TempPath) Then
                Exit Do
            End If

            If Directory.Exists(TempPath + "\Registries") Then

                PathRegistriesFolder = TempPath + "\Registries\"
                Exit Do

            Else

                ChDir("..")

            End If

        Loop Until TempPath.EndsWith(Application.ProductName.ToString)

        If PathRegistriesFolder = "" Then

            Directory.CreateDirectory(Application.StartupPath + "\Registries\")
            PathRegistriesFolder = Application.StartupPath + "\Registries\"

        End If

    End Sub

    Public Sub CheckXmlFiles()

        Dim TempPath As String
        Dim MyDirectory As DirectoryInfo
        Dim TempInitDirFilePath As String = ""
        Dim TempPrefFilePath As String = ""
        Dim ExistInitDirFile As Boolean
        Dim ExistPrefFile As Boolean

        Directory.SetCurrentDirectory(Application.StartupPath)

        Do
            TempPath = Directory.GetCurrentDirectory()
            MyDirectory = New DirectoryInfo(TempPath)

            If Directory.GetLogicalDrives.Contains(TempPath) Then
                Exit Do
            End If

            For Each MyFile In MyDirectory.GetFiles("*.xml")

                If MyFile.Name = "Init_Directories.xml" Then

                    ExistInitDirFile = True
                    TempInitDirFilePath = TempPath + "\"

                End If

                If MyFile.Name = "Preferences.xml" Then

                    ExistPrefFile = True
                    TempPrefFilePath = TempPath + "\"

                End If

            Next

            ChDir("..")

        Loop Until TempPath.EndsWith(Application.ProductName.ToString)

        If ExistInitDirFile And ExistPrefFile And TempInitDirFilePath = TempPrefFilePath Then

            PathXmlFiles = TempInitDirFilePath

        End If

    End Sub

    Public Function CreateFilePath() As String

        Dim Filepath As New StringBuilder
        Dim MyDirectory As DirectoryInfo
        Dim NomFile As String
        Dim FolderPath As String
        Dim NumFiles As Int16

        FolderPath = PathRegistriesFolder

        MyDirectory = New DirectoryInfo(FolderPath)

        Filepath.Append(FolderPath)

        Dim counter = MyDirectory.GetFiles("Output_*.txt")

        NumFiles = CShort(counter.Count + 1)

        NomFile = "Output_" + NumFiles.ToString + ".txt"

        Filepath.Append(NomFile)

        Return Filepath.ToString


    End Function

    Public Function FindFileInProject(ByVal MyFile As String, Optional ByVal AddDirectory As String = "") As String

        Dim MyPath As String

        MyPath = FindPathApplication()

        If AddDirectory <> "" Then

            MyPath = MyPath + "\" + AddDirectory + "\" + MyFile

        Else

            MyPath = MyPath + "\" + MyFile

        End If

        If Not FileExists(MyPath) Then

            MyPath = ""

        End If

        Return MyPath

    End Function

    Public Function FindPathApplication() As String

        Dim MyPath As String

        MyPath = CurrentDirectory

        Return MyPath

    End Function

    Public Sub CreateLogFile(ByVal texto As String)

        Dim Info As Byte()
        Dim LogFile As FileStream
        Dim PathFolder As String

        PathFolder = AccessFiles.PathRegistriesFolder

        If Not Directory.Exists(PathFolder) Then
            Directory.CreateDirectory(PathFolder)
        End If

        LogFile = File.Create(CreateFilePath)

        Info = New UTF8Encoding(True).GetBytes(texto)

        LogFile.Write(Info, 0, Info.Length)

        LogFile.Close()

    End Sub

    Public Sub ReadXml(ByRef DatSet As DataSet, ByVal MyFile As String)

        DatSet.ReadXml(MyFile, XmlReadMode.ReadSchema)

    End Sub

    Public Sub WriteXml(ByVal DatSet As DataSet, ByVal MyFile As String)

        DatSet.WriteXml(MyFile, XmlWriteMode.WriteSchema)

    End Sub

    Public Sub DeleteFiles()

        Dim MyDirectory As DirectoryInfo
        Dim PathFolder As String
        Dim MyFile As FileInfo

        PathFolder = PathRegistriesFolder

        If Not Directory.Exists(PathFolder) Then
            Exit Sub
        End If

        MyDirectory = New DirectoryInfo(PathFolder)

        For Each MyFile In MyDirectory.GetFiles()

            If (MyFile.CreationTime < DateSelected) Then

                MyFile.Delete()

            End If

        Next

    End Sub

End Module
